#!/usr/bin/python
import os,sys,re

# Modify Linux kernel `.config' file as follows:
# < # CONFIG_SYSFS_DEPRECATED_V2 is not set
# > CONFIG_SYSFS_DEPRECATED=y
# > CONFIG_SYSFS_DEPRECATED_V2=y
# < CONFIG_AEABI=y
# < # CONFIG_OABI_COMPAT is not set
# > # CONFIG_AEABI is not set
# > CONFIG_FPE_NWFPE=y
# > # CONFIG_FPE_NWFPE_XP is not set
# > # CONFIG_FPE_FASTFPE is not set
# > # CONFIG_ARTHUR is not set
# < # CONFIG_MTD_NAND_ATMEL_ECC_HW is not set
# < CONFIG_MTD_NAND_ATMEL_ECC_SOFT=y
# > CONFIG_MTD_NAND_ATMEL_ECC_HW=y
# > # CONFIG_MTD_NAND_ATMEL_ECC_SOFT is not set

if os.getenv('LINUX')==None: raise Exception('$LINUX not set.')
if os.getenv('SOURCE_DIR')==None: raise Exception('$SOURCE_DIR not set.')
os.chdir( os.path.join(os.getenv('SOURCE_DIR'), os.getenv('LINUX')) )

indices = xrange(sys.maxint)
f=open('.config','r')
contents=f.readlines()
f.close()

def match(s):
	found=-1
	for c, i in zip(contents, indices):
		if re.search(s, c):
			if found!=-1:
				raise Exception('more than two lines match.', s)
			found=i
	if found==-1: raise Exception('No line matches.', s)
	return found

replaces={
'CONFIG_SYSFS_DEPRECATED=y':'',
'CONFIG_SYSFS_DEPRECATED_V2=y':'# CONFIG_SYSFS_DEPRECATED_V2 is not set',
'# CONFIG_AEABI is not set':'CONFIG_AEABI=y\n# CONFIG_OABI_COMPAT is not set',
'CONFIG_FPE_NWFPE=y':'',
'# CONFIG_FPE_NWFPE_XP is not set':'',
'# CONFIG_FPE_FASTFPE is not set':'',
'# CONFIG_ARTHUR is not set':'',
'CONFIG_MTD_NAND_ATMEL_ECC_HW=y':'# CONFIG_MTD_NAND_ATMEL_ECC_HW is not set',
'# CONFIG_MTD_NAND_ATMEL_ECC_SOFT is not set':'CONFIG_MTD_NAND_ATMEL_ECC_SOFT=y',
}

for k in replaces.keys(): match(k) # check one match and only one match
for k in replaces.keys(): contents[match(k)]=replaces[k]+'\n'

f=open('.config','r')
for c in contents: f.write(c)
f.close()
